<!--
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-02-20 19:27:43
 * @Description  : 章节添加或者修改面板
-->

<template>
  <div class="bg-lightColor chapter-edit-con p-3 text-[13px]">
    <n-grid x-gap="12" :cols="1" class="mb-2">
      <n-gi>
        <n-button
          type="primary"
          size="small"
          class="!text-[13px]"
          @click="backView"
        >
          <div class="mr-2 rotate-90 transform">
            <SvgIcon icon="xiala" color="#fff" size="10px"></SvgIcon>
          </div>
          返回
        </n-button>
      </n-gi>
    </n-grid>
    <n-divider class="!my-2" />
    <n-form
      ref="chapterFormRef"
      :label-width="isSmall ? 100 : 120"
      label-placement="left"
      :label-align="isSmall ? 'left' : 'right'"
      :model="chapter"
      size="small"
      require-mark-placement="right"
      :rules="formRule"
    >
      <n-form-item label="章节名称：" path="name">
        <n-input
          v-model:value="chapter.name"
          :allow-input="noSideSpace"
          placeholder="请输入章节名称"
          clearable
          class="!w-[300px]"
          :maxlength="50"
        />
      </n-form-item>
      <n-form-item label="父级章节：" path="pid">
        <ChapterSelect
          class="!w-[300px]"
          :multiple="false"
          :default-exam-id="chapter.pid"
          @change-value="(value: any) => (chapter.pid = value)"
        ></ChapterSelect>
      </n-form-item>
      <n-form-item label="所属科目：" path="examId">
        <!-- <ExamSelect
          class="!w-[300px]"
          :multiple="false"
          :default-exam-id="chapter.examId"
          @change-value="(value: any) => (chapter.examId = value)"
        ></ExamSelect> -->
        <CommonSelect
          :has-chapter="false"
          :has-paper="false"
          :has-btn="false"
          @change-value="(value: any) => (chapter.examId = value.examId)"
        ></CommonSelect>
      </n-form-item>
      <n-form-item label=" " v-permission="['admin']">
        <n-button
          type="primary"
          class="grent-btn w-[120px]"
          @click="handleSave(false)"
        >
          保存
        </n-button>
        <n-button
          v-if="!isModify"
          type="primary"
          class="grent-btn w-[120px] ml-2"
          @click="handleSave(true)"
        >
          保存并新增
        </n-button>
      </n-form-item>
    </n-form>
  </div>
</template>

<script setup lang="ts">
import type { FormInst } from 'naive-ui'
import type { ChapterRes, ChapterUpdateReq } from '@/types/chapter'
import { noSideSpace } from '@/utils'
import { userMenuShow } from '@/hooks/useMenuShow'
const { isSmall } = userMenuShow()

const props = defineProps<{
  isModify: boolean
  chapterRes?: ChapterRes
}>()

const emits = defineEmits<{
  (e: 'backView'): void
  (
    e: 'changeChapter',
    type: number,
    data: ChapterUpdateReq,
    addNew: boolean
  ): void
}>()
const backView = () => {
  emits('backView')
}

const chapter = reactive<ChapterUpdateReq>({
  name: '',
  examId: '',
  pid: ''
})

const initChapter = () => {
  if (props.isModify) {
    const { name, pid, examId, id } = props.chapterRes as ChapterRes
    chapter.chapterId = id
    chapter.name = name
    chapter.pid = pid
    chapter.examId = examId
  }
}

onMounted(() => {
  initChapter()
})

const formRule = {
  name: [
    {
      required: true,
      message: '请输入章节名称',
      trigger: ['blur']
    },
    {
      min: 1,
      max: 50,
      trigger: ['blur'],
      message: '长度在1-50位'
    }
  ],
  pid: [
    {
      required: true,
      message: '请选择父级',
      trigger: ['blur']
    }
  ],
  examId: [
    {
      required: true,
      message: '请选择科目ID',
      trigger: ['blur']
    }
  ]
}

const chapterFormRef = ref<FormInst | null>(null)

const handleSave = (addNew = false) => {
  chapterFormRef.value?.validate((error: any) => {
    if (error) {
      return
    }

    if (props.isModify) {
      emits('changeChapter', 1, chapter, addNew)
    } else {
      emits('changeChapter', 0, chapter, addNew)
    }
    if (addNew) {
      chapter.name = ''
    }
  })
}
</script>

<style scoped lang="scss"></style>
